package com.cc.bill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cc.bill.dao.CardDao;
import com.cc.bill.entity.CardEntity;
import com.cc.bill.entity.CardPictureEntity;
import com.cc.bill.feign.UserFeign;
import com.cc.bill.service.CardPictureService;
import com.cc.bill.service.CardService;
import com.cc.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("cardService")
public class CardServiceImpl extends ServiceImpl<CardDao, CardEntity> implements CardService {

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private CardPictureService cardPictureService;


    @Override
    public CardEntity addCard(Long uid, String cardAddress, Integer cardType, String outworkComments, String workRemarks, List<String> imageUrls) {
        CardEntity cardEntity = new CardEntity();
        cardEntity.setCardAddress(cardAddress);
        cardEntity.setCardType(cardType);
        cardEntity.setCreatetime(new Date());
        if(!StringUtils.isEmpty(outworkComments)){
            cardEntity.setOutworkComments(outworkComments); // todo 是否有外出打卡原因
        }
        cardEntity.setUid(uid);
        if(!StringUtils.isEmpty(workRemarks)){
            cardEntity.setWorkRemarks(workRemarks); // todo 是否有备注
        }
        baseMapper.insert(cardEntity);
        if(cardType == 0){
            // 外出打卡
            if(imageUrls != null && imageUrls.size()>0){
                for (String imageUrl : imageUrls) {
                    CardPictureEntity cardPictureEntity = new CardPictureEntity(); // 添加图片
                    cardPictureEntity.setCid(cardEntity.getCid());
                    cardPictureEntity.setPicture(imageUrl);
                    cardPictureService.save(cardPictureEntity);
                    cardEntity.getImageUrls().add(imageUrl);
                }
            }

        }
        return cardEntity;
    }

    @Override
    public List<CardEntity> getKQ(Long uid, String begin, String end) throws ParseException {

        QueryWrapper<CardEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        queryWrapper.orderByDesc("createtime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(!StringUtils.isEmpty(begin)){
            begin = begin+" 00:00:00";
            queryWrapper.ge("createtime",sdf.parse(begin));
        }
        if(!StringUtils.isEmpty(end)){
            end = end+" 23:59:00";
            queryWrapper.le("createtime",sdf.parse(end));
        }
        List<CardEntity> cardEntities = baseMapper.selectList(queryWrapper);
        return cardEntities;
    }

    @Override
    public Page<CardEntity> getAllKQ(Map<String, Object> params) throws ParseException {
        String begin = (String) params.get("begin");
        String end = (String) params.get("end");
        String nickname = (String) params.get("nickname");
        String mobile = (String) params.get("mobile");
        Page<CardEntity> page = new Page<>(Long.parseLong((String)params.get("page")),
                Long.parseLong((String)params.get("limit")));
        QueryWrapper<CardEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(!StringUtils.isEmpty(begin)){
            begin = begin+" 00:00:00";
            queryWrapper.ge("createtime",sdf.parse(begin));
        }
        if(!StringUtils.isEmpty(end)){
            end = end+" 23:59:00";
            queryWrapper.le("createtime",sdf.parse(end));
        }

        if(!(StringUtils.isEmpty(mobile) && StringUtils.isEmpty(nickname))) {
            UserEntity user = userFeign.getUserByMobileOrNickname(mobile, nickname);
            if(user != null){
                if(user.getPermission() == 1){
                    // 员工
                    queryWrapper.eq("uid",user.getUid());
                }else {
                    // 客户
                    return page;
                }
            }else {
                return page;
            }
        }
        /*if(nickname != null){
            queryWrapper.eq("uid",Long.parseLong(nickname));
        }*/
        baseMapper.selectPage(page,queryWrapper);
        List<CardEntity> records = page.getRecords();
        for (CardEntity record : records) {
            List<CardPictureEntity> cardPictureEntities = cardPictureService.list(new QueryWrapper<CardPictureEntity>().eq("cid", record.getCid()));
            if(cardPictureEntities != null && cardPictureEntities.size()>0){
                for (CardPictureEntity cardPictureEntity : cardPictureEntities) {
                    record.getImageUrls().add(cardPictureEntity.getPicture());
                }
            }
        }
        page.setRecords(records);
        System.out.println(page.getRecords());
        return page;
    }
}